import GChild from "../../../Core/GView/GChild";
import MathEx from "../../../Core/Math/MathEx";
import ShaderUtil from "../../../Shaders/Manager/ShaderUtil";
import { JXColor } from "../../Common/JXColor";
import { VIEW_ID } from '../../Common/UI';
import GameMgr from "../../Logic/GameMgr";
import { LinearState, PART_BTL_SCENE } from './../../Common/Define';
import { Res } from './../../Common/UIResources';
import { JXLocales } from './../../Common/Zh';
const { ccclass, property, executionOrder, menu } = cc._decorator;
@ccclass @menu('View/main/LevelItem')
export default class LevelItem extends GChild {
	@property(cc.Node) stars: cc.Node = null;
	@property(cc.Sprite) img: cc.Sprite = null;
	@property(cc.Label) lv: cc.Label = null;

	@property({ type: cc.Node, tooltip: "刀子" }) skill: cc.Node = null;

	private _curStar: number = 0;
	private _levelData: SStoryDataRaw = null;
	private _arrowNodes: cc.Node[] = [];
	private _isTouchStart: boolean = false;
	private _isTouchMoved: boolean = false;
	protected _state: LinearState = LinearState.Pass;
	protected _listView: cc.ScrollView = null;

	public get levelData() {
		return this._levelData;
	}

	/** 是否为最后一关 */
	private _isLast: boolean = false;
	public onGLoad(): void {
		this.node.on(cc.Node.EventType.TOUCH_START, this.touchBegin, this);
		this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
		this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
		this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
	}

	private touchBegin(event: cc.Event.EventTouch) {
		this._isTouchStart = true;
		this._listView.enabled = false;
	}

	private touchMove(event: cc.Event.EventTouch) {
		if (!this._isTouchStart) return;
		if (MathEx.getDistance(event.getStartLocation(), event.getLocation()) < 3) return;
		this._isTouchMoved = true;

		if (CC_DEBUG && WD_DEBUG) {
			this.node.position = this.node.position.add(event.getDelta());
		}
	}

	private touchEnd(evnet: cc.Event.EventTouch) {
		// if (!this._isTouchStart) return;
		if (this._isTouchMoved && this._isTouchStart) {
			if (CC_DEBUG && WD_DEBUG) {
				this._DEBUG_checkArrows();
			}
		} else {
			this.onItemClick();
		}
		this._isTouchStart = false;
		this._isTouchMoved = false;
		this._listView.enabled = true;
	}

	public init(levelData: SStoryDataRaw, index: number, points: cc.Node[], length: number, view: cc.ScrollView) {
		this._listView = view;
		this._levelData = levelData;
		this.node.name = "ITEM_" + levelData.id;
		this.lv.string = '第' + levelData.id.toString() + '关';
		this._arrowNodes = points;
		this._isLast = length === index + 1;
		this.changeItemState();
	}

	protected _DEBUG_checkArrows() {
		let c = this.node.parent.children;
		let index = c.indexOf(this.node);
		if (index != 0 && this._arrowNodes.length > 0) {
			let from = c[index - 1].position;
			let to = this.node.position;
			this._DEBUG_reSetArrows(from, to);
		}
		if (index != c.length - 1) {
			let next = c[index + 1].getComponent(LevelItem);
			if (next._arrowNodes.length > 0) {
				let from = this.node.position;
				let to = next.node.position;
				next._DEBUG_reSetArrows(from, to);
			}
		}
	}

	protected _DEBUG_reSetArrows(from: cc.Vec2, to: cc.Vec2) {
		let points = []
		let dis = MathEx.getDistance(from, to) + 25;
		let num = Math.floor(dis / 25);
		for (let i = 1; i < num - 1; i++) {
			let result = from.lerp(to, (i + 0.5) / num);
			points.push(result);
		}

		for (let i = 0; i < points.length; i++) {
			let node = this._arrowNodes[i];
			if (!node) {
				node = cc.instantiate(this._arrowNodes[0]);
				node.parent = this._arrowNodes[0].parent;
				this._arrowNodes.push(node);
			}
			node.position = points[i];
		}
		for (let i = points.length; i < this._arrowNodes.length; i++) {
			this._arrowNodes[i].destroy();
		}
		this._arrowNodes.length = points.length;
	}

	public startInit(isAction: boolean, endCb: any) {
		let setStateCb = () => {
			let socroll = this.node.parent.parent.parent.parent.getComponent(cc.ScrollView);

			if (this._state == LinearState.Pass) {
				this.changeItemState();
			} else if (this._state == LinearState.Now) {
				this.changeItemState();
			}

			let halfHeight = cc.winSize.height / 2;
			if (this.node.y > halfHeight && (this._state == LinearState.Pass || this._state == LinearState.Now)) {
				socroll.scrollToOffset(cc.v2(0, halfHeight - this.node.position.y), 1);
			}
			if (this._isLast) {
			}
		}

		if (isAction) {
			this.node.runAction(
				cc.sequence(
					cc.callFunc(() => {
						if (this._arrowNodes.length) {
							for (let i = 0; i < this._arrowNodes.length; i++) {
								this._arrowNodes[i].runAction(cc.fadeIn(0.015 * (i + 1)));
							}
						}
					}),
					cc.delayTime(0.005 * this._arrowNodes.length),
					cc.callFunc(setStateCb),
					cc.fadeIn(0.01),
					cc.callFunc(() => endCb && endCb())
				)
			);
		} else {
			setStateCb();
			endCb();
		}

	}

	public changeItemState() {
		this._curStar = GameMgr.lUserData.getStarByLevel(this.levelData.id);
		this.stars.children.forEach((star, index) => {
			if (this._curStar < index + 1) {
				ShaderUtil.gray(star)
			} else {
				ShaderUtil.normal(star)
			}
		})
		let curLvId = GameMgr.lUserData.level + 1;
		if (this._levelData.id < curLvId) {
			this._state = LinearState.Pass;
		} else if (this._levelData.id == curLvId) {
			this._state = LinearState.Now;
		} else {
			this._state = LinearState.Future;
		}

		if (this._state == LinearState.Now) {
			// this.assetImpl.spriteAtlasFrame(this.img, Res.texture.views.homeUI, "ll_now");
			this.img.node.active = false;
			this.skill.active = true;
			// this.lv.node.position = cc.v2(-73, 11)
		} else {
			this.img.node.active = true;
			this.assetImpl.spriteAtlasFrame(this.img, Res.texture.views.homeUI, "ll_un_pass")
			// this.lv.node.position = cc.v2(-70, 35)
			this.lv.node.color = JXColor.Cffffff;
		}

	}


	/** 播放进入当前关卡 */
	public playIntoAni(endCb: any) {

		let points = this._arrowNodes;
		let tween = cc.tween(this.node).delay(0.2);
		for (let i = 0; i < points.length; i++) {
			tween = tween.to(0.15, { position: points[i] }).call(() => {
				this._arrowNodes[i] && this.assetImpl.spriteAtlasFrame(this._arrowNodes[i].getComponent(cc.Sprite), Res.texture.views.homeUI, "point_1");
			});
		}
		tween.call(() => {
			endCb();
		}).start();
	}

	protected onItemClick() {
		if (this._state == LinearState.Now || this._state == LinearState.Pass) {
			let isNoHero = GameMgr.rHeroData.checkInNoHero();
			if (isNoHero) {
				GameMgr.uiMgr.showToast(JXLocales.fight.teamEmpty);
				return;
			}
			let args: ArgsBattleViewCtrl<SceneGuanQia> = {
				sceneId: PART_BTL_SCENE.GUANQIA,
				args: { level: this.levelData.id }
			}
			GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.fightLayer, args)
		} else {
			return;
		}
	}

}